package com.example.zyfypt_423zhy.model;




import com.example.zyfypt_423zhy.bean.ProjectBean;
import com.example.zyfypt_423zhy.bean.VideoBean;
import com.example.zyfypt_423zhy.iface.ProjectListener;
import com.example.zyfypt_423zhy.iface.Projectiface;
import com.example.zyfypt_423zhy.iface.VideoListener;
import com.example.zyfypt_423zhy.iface.Videoiface;
import com.example.zyfypt_423zhy.service.ProjectService;
import com.example.zyfypt_423zhy.service.VideoService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 大玉儿 on 2017/4/20.
 */
//model 获取文章数据
public class ProjectModel implements Projectiface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public ProjectModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据
    @Override
    public void getResultList(String project, int page, String sessionID, final ProjectListener projectListener) {
        //使用Retrofit-2
        ProjectService projectService=retrofit.create(ProjectService.class);
        Call<List<ProjectBean>> call
                =projectService.getResult(project,page,sessionID);
        //使用Retrofit-3
        call.enqueue(new Callback<List<ProjectBean>>() {
            @Override
            public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    projectListener.onResponse(response.body());
                }
                else
                    projectListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                projectListener.onFail(t.toString());
            }
        });
    }
}
